package com.health.fragment.tijian;

import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.PersonalFileActivity.onFragmentSave;
import com.health.R;
import com.health.adapter.RadioButtonAdapter;
import com.health.db.FDbManager;
import com.health.fragment.NovaFragment;
import com.health.tablebean.ARCHIVE_ASSISTCHECK;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.BD_DD_INFO;
import com.health.tablebean.SBCL_DATA;
import com.health.viewscontrols.tijian.TiJianActivity;
import com.health.tools.GlobalConts;
import com.health.widget.MyGridView;
import com.novabluetooth.services.BluetoothConstant;
import com.novabluetooth.services.lbxd.ShowPicActivity;
import com.novabluetooth.views.ExamHistoryActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

/**
 * 健康体检 辅助检查
 * 
 * @author 鹿云龙
 */
public class HealthTiJian_FzjcFragment extends NovaFragment implements onFragmentSave, OnClickListener {
	private View baseView;
	private ARCHIVE_BASEINFO userInfo;// 基本信息
	private ARCHIVE_ASSISTCHECK assistCheck;//
	// *---------------------
	/** 血红蛋白 */
	private static EditText etHB;
	/** 白细胞 */
	private EditText etWBC;
	/** 血小板 */
	private EditText etPLT;
	/** 血常规其他 */
	private EditText etBLOOD_OTHER;
	/** 空腹血糖mmol/L */
	public static EditText etFPGL;
	/** 空腹血糖mg/dL */
	public static EditText etFPGL2;

	/** 尿常规，蛋白质/葡萄糖/酮体/潜血 */
	private static EditText etNiaoChangGui1;
	private static EditText etNiaoChangGui2;
	private static EditText etNiaoChangGui3;
	private static EditText etNiaoChangGui4;
	/** 尿常规其他 */
	private EditText etURINE_OTHER;
	/** 尿微量白蛋白 */
	private EditText etALBUMIN;
	/** 糖化血红蛋白 */
	private EditText etHBALC;
	private EditText et_xindiantu;
	/** 心电图 */
	private RadioGroup rg_ecg;
	private RadioButton rd_normal;
	private RadioButton rd_unnormal;

	/** 大便潜血 */
	private MyGridView mgv_DaBianQianXue;
	/** 大便潜血 */
	private RadioButtonAdapter rba_FOB;
	/** 乙型肝炎表面抗原 */
	private MyGridView mgv_YiXingGanYanBiaoMianKangYuan;
	/** 乙型肝炎表面抗原 */
	private RadioButtonAdapter rba_HBSAG;
	// *---------------------
	// 肝功能---------------------
	/** 血清谷丙转氨酶 */
	private EditText etSGPT;
	/** 血清谷草转氨酶 */
	private EditText etGOT;
	/** 白蛋白 */
	private EditText etBP;
	/** 结合胆红素 */
	private EditText etCB;
	/** 总胆红素 */
	private EditText etTBIL;
	// 肾功能---------------------
	/** 血清肌酐 */
	private EditText etSCR;
	/** 血尿素氮 */
	private EditText etBUN;
	/** 血钾浓度 */
	private EditText etPC;
	/** 血钠浓度 */
	private EditText etHYPE;
	// 血脂---------------------
	/** 总胆固醇 */
	private EditText etTC;
	/** 血清低密度脂蛋白胆固醇 */
	private EditText etLOW_CHO;
	/** 甘油三酯 */
	private EditText etTG;
	/** 血清高密度脂蛋白胆固醇 */
	private EditText etHEI_CHO;
	/** 计算血清低密度脂蛋白胆固醇 */
	private Button btn_jisuan;
	// *-------------------
	/** 胸部X线片 */
	private MyGridView mgv_XiongBuXXianPian;
	/** 胸部X线片 */
	private RadioButtonAdapter rba_XiongBuXXianPian;
	/** B超 */
	private MyGridView mgv_BChao;
	/** B超 */
	private RadioButtonAdapter rba_BChao;
	/** 腹部B超 */
	private MyGridView mgv_fbBChao;
	/** 腹部B超 */
	private RadioButtonAdapter rba_fbBChao;
	/** 宫颈涂片 */
	private MyGridView mgv_GongJingTuPian;
	/** 宫颈涂片 */
	private RadioButtonAdapter rba_GongJingTuPian;
	private EditText et_xiongbuxxianpian;
	private EditText et_bchao;
	private EditText et_fbbchao;
	private EditText et_gongjingtupian;

	// clear button
	private Button btn_xindiantu;
	private Button btn_dabianqianxue;
	private Button btn_yixingganyan;

	private Button btn_xiongbu;
	private Button btn_bchao;
	private Button btn_fbbchao;
	private Button btn_gongjing;

	private int from = 0;
	// x片
	private Button niaochanggui_btn;// 尿常规
	private Button xhdb_btn;// 血红蛋白
	private Button xuetang_btn;// 血糖
	private Button ecg_btn;// 心电图

	private TextView tv_ecg;
	private String str_ecgId;
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	// 其他
	private EditText ed_qt_last;

	private RadioButton rb_xbzc;
	private RadioButton rb_xbyc;
	private RadioButton rb_gjzc;
	private RadioButton rb_gjyc;
	private RadioButton rb_bczc;
	private RadioButton rb_bcyc;
	private RadioButton rb_bcqtzc;
	private RadioButton rb_bcqtyc;
	private RadioGroup rg_xb;
	private RadioGroup rg_gj;
	private RadioGroup rg_bc;
	private RadioGroup rg_bcqt;
	private LinearLayout ll_gjtp;
	private Button dgc_btn;

	@SuppressLint("InflateParams")
	@Override
	public View findViews(LayoutInflater inflater) {
		baseView = inflater.inflate(R.layout.fragment_healthtijian_fzjc, null);
		userInfo = NovaApplication.getUserInfo();
		findViews(baseView);
		if ("DM01-08_1".equals(userInfo.getSEX())) {
			ll_gjtp.setVisibility(View.GONE);
		}

		// GlobalConts.setMaxLength(5, etFPGDL);
		GlobalConts.setMaxLength(5, etHBALC, etSGPT, etGOT, etBP, etCB, etTBIL, etSCR, etBUN, etPC, etHYPE);
		GlobalConts.setMaxLength(6, etALBUMIN, etTC, etLOW_CHO, etTG, etHEI_CHO);
		GlobalConts.setMaxLength(5, etHB, etWBC, etPLT);
		GlobalConts.setMaxLength(50, etBLOOD_OTHER, etURINE_OTHER, et_xindiantu, et_xiongbuxxianpian, et_bchao,
				et_fbbchao, et_gongjingtupian);
		// GlobalConts.setMaxLength(29, etNiaoChangGui);
		return baseView;
	}

	@Override
	public void setupView(View view) {
	}

	@Override
	public void setData() {
		bind1();
		bind2();
		initData();
		GlobalConts.keep2decimal(etFPGL);
		GlobalConts.keep2decimal(etFPGL2);

	}

	@Override
	public void onResume() {
		super.onResume();
		// setData();
	}

	@Override
	public void addListener() {
		rg_ecg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.rd_normal) {
					str_ecgId = "DM100-15_1";
					et_xindiantu.setEnabled(false);
					et_xindiantu.setText("");
				} else if (checkedId == R.id.rd_unnormal) {
					str_ecgId = "DM100-15_2";
					et_xindiantu.setEnabled(true);
				} else {
					str_ecgId = "";
				}
			}
		});

		btn_xindiantu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				rg_ecg.clearCheck();
				et_xindiantu.setText("");
			}
		});
		btn_dabianqianxue.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				rba_FOB.clerChecked();
			}
		});
		btn_yixingganyan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				rba_HBSAG.clerChecked();
			}
		});

		btn_xiongbu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				rba_XiongBuXXianPian.clerChecked();
				et_xiongbuxxianpian.setText("");
				rg_xb.clearCheck();
			}
		});
		btn_bchao.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				rba_BChao.clerChecked();
				et_bchao.setText("");
				rg_bcqt.clearCheck();
			}
		});
		btn_fbbchao.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				rba_fbBChao.clerChecked();
				et_fbbchao.setText("");
				rg_bc.clearCheck();
			}
		});
		btn_gongjing.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				rba_GongJingTuPian.clerChecked();
				et_gongjingtupian.setText("");
				rg_gj.clearCheck();
			}
		});
		btn_jisuan.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					if (etTC.getText().toString().isEmpty() || etTG.getText().toString().isEmpty()
							|| etHEI_CHO.getText().toString().isEmpty()) {
						return;
					}
					double tc = Double.parseDouble(etTC.getText().toString());
					double tg = Double.parseDouble(etTG.getText().toString());
					double hdl = Double.parseDouble(etHEI_CHO.getText().toString());
					java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
					etLOW_CHO.setText(df.format(GlobalConts.calcLdl(tc, tg, hdl)));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		niaochanggui_btn.setOnClickListener(this);
		xhdb_btn.setOnClickListener(this);
		xuetang_btn.setOnClickListener(this);
		ecg_btn.setOnClickListener(this);
        dgc_btn.setOnClickListener(this);
	}

	public int getListIndex(String id, List<BD_DD_INFO> bds) {
		int index = 0;
		try {
			for (int i = 0; i < bds.size(); i++) {
				if (id.equals(bds.get(i).getID())) {
					index = i;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return index;
	}

	/**
	 * 字符串转换"1,2,3,4,5" --> "'1','2','3','4','5'"
	 * 
	 * @param symptom
	 * @return
	 * @author 鹿云龙
	 * @date 2015年12月19日 下午2:49:23
	 */
	public String checkedIdsSql(String symptom) {
		String[] idarr = symptom.split(",");
		String sql = "";
		for (String id : idarr) {
			sql += "'" + id + "',";
		}
		sql = sql.substring(0, sql.length() - 1);
		System.out.println(sql);
		return sql;
	}

	@Override
	public void save() {
		saveData();
	}

	public void refeshData(String xuetang) {
		etFPGL.setText(xuetang);
	}

	private void findViews(View baseView) {
		btn_jisuan = (Button) baseView.findViewById(R.id.btn_jisuan);
		etHB = (EditText) baseView.findViewById(R.id.et_xuehongdanbai);// 血红蛋白
		etWBC = (EditText) baseView.findViewById(R.id.et_baixibao);// 白细胞
		etPLT = (EditText) baseView.findViewById(R.id.et_xuexiaoban);// 血小板
		etBLOOD_OTHER = (EditText) baseView.findViewById(R.id.et_xuechangguiqita);// 血常规其他
		ed_qt_last = (EditText) baseView.findViewById(R.id.ed_qt_last);
		ll_gjtp = (LinearLayout) baseView.findViewById(R.id.ll_gjtp);

		etFPGL = (EditText) baseView.findViewById(R.id.et_kongfuxuetang);// 空腹血糖mmol/L
		etFPGL2 = (EditText) baseView.findViewById(R.id.et_kongfuxuetang2);// 空腹血糖mg/dL
		etFPGL.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				String str_yc = etFPGL.getText().toString();
				if (!TextUtils.isEmpty(str_yc)) {

					double int_yc = Double.parseDouble(str_yc);
					if (int_yc > 30) {
						etFPGL.setBackgroundColor(getResources().getColor(R.color.orange));
					} else {
						etFPGL.setBackgroundColor(Color.WHITE);
					}
					if (int_yc > 100) {
						etFPGL.setText("");
						GlobalConts.showToast(getActivity(), "输入错误，请重输");
					}
					if (int_yc < 100) {
						DecimalFormat formater = new DecimalFormat();
						etFPGL2.setText(formater.format(int_yc * 18));
					}

				} else {
					etFPGL.setBackgroundColor(Color.WHITE);
				}
			}
		});
		etFPGL2.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (".".equals(s.toString())) {
					etFPGL2.setText("");
					GlobalConts.showToast(getActivity(), "输入错误，请重输");
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				String str_yc = etFPGL2.getText().toString();
				if (!TextUtils.isEmpty(str_yc)) {
					String str_yc1 = str_yc.replace(",", "");
					double int_yc = Double.parseDouble(str_yc1);
					if (int_yc > 30 * 18) {
						etFPGL2.setBackgroundColor(getResources().getColor(R.color.orange));
					} else {
						etFPGL2.setBackgroundColor(Color.WHITE);
					}
					if (int_yc > 100 * 18) {
						etFPGL2.setText("");
						GlobalConts.showToast(getActivity(), "输入错误，请重输");
					}

				} else {
					etFPGL2.setBackgroundColor(Color.WHITE);
				}
			}
		});
		etNiaoChangGui1 = (EditText) baseView.findViewById(R.id.et_niaochanggui1);// 尿常规
		etNiaoChangGui2 = (EditText) baseView.findViewById(R.id.et_niaochanggui2);// 尿常规
		etNiaoChangGui3 = (EditText) baseView.findViewById(R.id.et_niaochanggui3);// 尿常规
		etNiaoChangGui4 = (EditText) baseView.findViewById(R.id.et_niaochanggui4);// 尿常规
		etURINE_OTHER = (EditText) baseView.findViewById(R.id.et_niaochangguiqita);// 尿常规其他
		etALBUMIN = (EditText) baseView.findViewById(R.id.et_niaoweiliangbaidanbai);// 尿微量白蛋白
		etHBALC = (EditText) baseView.findViewById(R.id.et_tanghuaxuehongdanbai);// 糖化血红蛋白
		et_xindiantu = (EditText) baseView.findViewById(R.id.et_xindiantu);
		mgv_DaBianQianXue = (MyGridView) baseView.findViewById(R.id.mgv_dabian);
		mgv_YiXingGanYanBiaoMianKangYuan = (MyGridView) baseView.findViewById(R.id.mgv_yixingganyan);
		rg_ecg = (RadioGroup) baseView.findViewById(R.id.rg_ecg);
		rd_normal = (RadioButton) baseView.findViewById(R.id.rd_normal);
		rd_unnormal = (RadioButton) baseView.findViewById(R.id.rd_unnormal);

		// *---------------------
		// 肝功能
		etSGPT = (EditText) baseView.findViewById(R.id.et_xueqinggubingzhuananmei);// 血清谷丙转氨酶
		etGOT = (EditText) baseView.findViewById(R.id.et_xueqinggucaozhuananmei);// 血清谷草转氨酶
		etBP = (EditText) baseView.findViewById(R.id.et_baidanbai);// 白蛋白
		etCB = (EditText) baseView.findViewById(R.id.et_jiehedanhongsu);// 结合胆红素
		etTBIL = (EditText) baseView.findViewById(R.id.et_zongdanhongsu);// 总胆红素
		// 肾功能
		etSCR = (EditText) baseView.findViewById(R.id.et_xueqingjigan);// 血清肌酐
		etBUN = (EditText) baseView.findViewById(R.id.et_xueniaosudan);// 血尿素氮
		etPC = (EditText) baseView.findViewById(R.id.et_xuejianongdu);// 血钾浓度
		etHYPE = (EditText) baseView.findViewById(R.id.et_xuenanongdu);// 血钠浓度
		// 血脂
		etTC = (EditText) baseView.findViewById(R.id.et_zongdanguchun);// 总胆固醇
		etLOW_CHO = (EditText) baseView.findViewById(R.id.et_xueqingdimiduzhidanbaidanguchun);// 血清低密度脂蛋白胆固醇
		etTG = (EditText) baseView.findViewById(R.id.et_ganyousanzhi);// 甘油三酯
		etHEI_CHO = (EditText) baseView.findViewById(R.id.et_xueqinggaomiduzhidanbaidanguchun);// 血清高密度脂蛋白当固醇
		dgc_btn=(Button) baseView.findViewById(R.id.dgc_btn);
		GlobalConts.keep2decimal(etTC, etLOW_CHO, etTG, etHEI_CHO);
		// *
		mgv_XiongBuXXianPian = (MyGridView) baseView.findViewById(R.id.mgv_xiongbuxxianpian);
		mgv_BChao = (MyGridView) baseView.findViewById(R.id.mgv_bchao);
		mgv_fbBChao = (MyGridView) baseView.findViewById(R.id.mgv_fbbchao);
		mgv_GongJingTuPian = (MyGridView) baseView.findViewById(R.id.mgv_gongjingtupian);

		et_xiongbuxxianpian = (EditText) baseView.findViewById(R.id.et_xiongbuxxianpian);
		et_bchao = (EditText) baseView.findViewById(R.id.et_bchao);
		et_fbbchao = (EditText) baseView.findViewById(R.id.et_fbbchao);
		et_gongjingtupian = (EditText) baseView.findViewById(R.id.et_gongjingtupian);
		btn_xindiantu = (Button) baseView.findViewById(R.id.btn_xindianchongzhi);
		btn_dabianqianxue = (Button) baseView.findViewById(R.id.btn_dabianchongzhi);
		btn_yixingganyan = (Button) baseView.findViewById(R.id.btn_yixingganyanchongzhi);

		btn_xiongbu = (Button) baseView.findViewById(R.id.btn_xiongbu);
		btn_bchao = (Button) baseView.findViewById(R.id.btn_bchao);
		btn_fbbchao = (Button) baseView.findViewById(R.id.btn_fbbchao);

		btn_gongjing = (Button) baseView.findViewById(R.id.btn_gongjing);

		niaochanggui_btn = (Button) baseView.findViewById(R.id.niaochanggui_btn);
		xhdb_btn = (Button) baseView.findViewById(R.id.xhdb_btn);
		xuetang_btn = (Button) baseView.findViewById(R.id.xuetang_btn);
		ecg_btn = (Button) baseView.findViewById(R.id.ecg_btn);
		tv_ecg = (TextView) baseView.findViewById(R.id.tv_ecg);

		rb_xbzc = (RadioButton) baseView.findViewById(R.id.rb_xbzc);
		rb_xbyc = (RadioButton) baseView.findViewById(R.id.rb_xbyc);
		rb_gjzc = (RadioButton) baseView.findViewById(R.id.rb_gjzc);
		rb_gjyc = (RadioButton) baseView.findViewById(R.id.rb_gjyc);
		rb_bczc = (RadioButton) baseView.findViewById(R.id.rb_bczc);
		rb_bcyc = (RadioButton) baseView.findViewById(R.id.rb_bcyc);
		rb_bcqtzc = (RadioButton) baseView.findViewById(R.id.rb_bcqtzc);
		rb_bcqtyc = (RadioButton) baseView.findViewById(R.id.rb_bcqtyc);

		rg_xb = (RadioGroup) baseView.findViewById(R.id.rg_xb);
		rg_gj = (RadioGroup) baseView.findViewById(R.id.rg_gj);
		rg_bc = (RadioGroup) baseView.findViewById(R.id.rg_bc);
		rg_bcqt = (RadioGroup) baseView.findViewById(R.id.rg_bcqt);

		rg_xb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.rb_xbzc) {
					et_xiongbuxxianpian.setEnabled(false);
					et_xiongbuxxianpian.setText("");
				} else if (checkedId == R.id.rb_xbyc) {
					et_xiongbuxxianpian.setEnabled(true);
				}
			}
		});

		rg_gj.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.rb_gjzc) {
					et_gongjingtupian.setEnabled(false);
					et_gongjingtupian.setText("");
				} else if (checkedId == R.id.rb_gjyc) {
					et_gongjingtupian.setEnabled(true);
				}
			}
		});

		rg_bc.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.rb_bczc) {
					et_fbbchao.setEnabled(false);
					et_fbbchao.setText("");
				} else if (checkedId == R.id.rb_bcyc) {
					et_fbbchao.setEnabled(true);
				}
			}
		});

		rg_bcqt.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.rb_bcqtzc) {
					et_bchao.setEnabled(false);
					et_bchao.setText("");
				} else if (checkedId == R.id.rb_bcqtyc) {
					et_bchao.setEnabled(true);
				}
			}
		});
	}

	private void bind1() {
		// 大便潜血
		List<BD_DD_INFO> lst_DaBianQianXue = FDbManager.getInstance(getActivity())
				.findAllByWhereInInfoDB(BD_DD_INFO.class, "parentid='DM100-39'");
		rba_FOB = new RadioButtonAdapter(lst_DaBianQianXue, getActivity());
		mgv_DaBianQianXue.setAdapter(rba_FOB);
		// 异性肝炎表面抗原
		List<BD_DD_INFO> lst_YiXingGanYanBiaoMianKangYuan = FDbManager.getInstance(getActivity())
				.findAllByWhereInInfoDB(BD_DD_INFO.class, "parentid='DM100-39'");
		rba_HBSAG = new RadioButtonAdapter(lst_YiXingGanYanBiaoMianKangYuan, getActivity());
		mgv_YiXingGanYanBiaoMianKangYuan.setAdapter(rba_HBSAG);
	}

	private void bind2() {
		List<BD_DD_INFO> lst_2 = FDbManager.getInstance(getActivity()).findAllByWhereInInfoDB(BD_DD_INFO.class,
				"parentid='DM100-15'");

		// 胸部X线片
		rba_XiongBuXXianPian = new RadioButtonAdapter(lst_2, getActivity());
		rba_XiongBuXXianPian.setOtherTextVisible(false);
		mgv_XiongBuXXianPian.setAdapter(rba_XiongBuXXianPian);
		// B超
		rba_BChao = new RadioButtonAdapter(lst_2, getActivity());
		rba_BChao.setOtherTextVisible(false);
		mgv_BChao.setAdapter(rba_BChao);

		// 腹部B超
		rba_fbBChao = new RadioButtonAdapter(lst_2, getActivity());
		rba_fbBChao.setOtherTextVisible(false);
		mgv_fbBChao.setAdapter(rba_fbBChao);
		// 宫颈涂片
		rba_GongJingTuPian = new RadioButtonAdapter(lst_2, getActivity());
		rba_GongJingTuPian.setOtherTextVisible(false);
		mgv_GongJingTuPian.setAdapter(rba_GongJingTuPian);
	}

	/**
	 * 获取下拉框中需要的数据的信息
	 * 
	 * @param datas
	 * @return
	 * @author 鹿云龙
	 * @date 2015年12月16日 下午3:47:00
	 */
	@SuppressWarnings("unused")
	private ArrayList<String> getDataInfo(List<BD_DD_INFO> datas) {
		ArrayList<String> infos = new ArrayList<String>();
		for (BD_DD_INFO bd : datas) {
			infos.add(bd.getDATAINFO());
		}
		return infos;
	}

	/**
	 * 初始化控件数据
	 * 
	 * @author 鹿云龙
	 * @date 2015年12月21日 上午10:10:19
	 */
	public void initData() {
		refeshData(NovaApplication.xuetang);
		List<ARCHIVE_ASSISTCHECK> assistChecks = FDbManager.getInstance(getActivity())
				.findAllByWhere(ARCHIVE_ASSISTCHECK.class, "IDCARD = '" + userInfo.getIDCARD() + "'");
		// findAllBTByWhere
		List<SBCL_DATA> xtList = FDbManager.getInstance(getActivity()).findAllByWhere(SBCL_DATA.class,
				"IDCARD = '" + userInfo.getIDCARD() + "' and " + "CL_TYPE='血糖'" + " and " + "CL_TIME like '"
						+ dateFormat.format(new Date()) + "%'");
		List<SBCL_DATA> dbzList = FDbManager.getInstance(getActivity()).findAllByWhere(SBCL_DATA.class,
				"IDCARD = '" + userInfo.getIDCARD() + "' and " + "CL_TYPE='蛋白质'" + " and " + "CL_TIME like '"
						+ dateFormat.format(new Date()) + "%'");
		List<SBCL_DATA> pttList = FDbManager.getInstance(getActivity()).findAllByWhere(SBCL_DATA.class,
				"IDCARD = '" + userInfo.getIDCARD() + "' and " + "CL_TYPE='葡萄糖'" + " and " + "CL_TIME like '"
						+ dateFormat.format(new Date()) + "%'");
		List<SBCL_DATA> ttList = FDbManager.getInstance(getActivity()).findAllByWhere(SBCL_DATA.class,
				"IDCARD = '" + userInfo.getIDCARD() + "' and " + "CL_TYPE='酮体'" + " and " + "CL_TIME like '"
						+ dateFormat.format(new Date()) + "%'");
		List<SBCL_DATA> qxList = FDbManager.getInstance(getActivity()).findAllByWhere(SBCL_DATA.class,
				"IDCARD = '" + userInfo.getIDCARD() + "' and " + "CL_TYPE='潜血'" + " and " + "CL_TIME like '"
						+ dateFormat.format(new Date()) + "%'");
		List<SBCL_DATA> hqPList = FDbManager.getInstance(getActivity()).findAllByWhere(SBCL_DATA.class,
				"IDCARD = '" + userInfo.getIDCARD() + "' and " + "CL_TYPE='理邦心电图片'" + " and " + "CL_TIME like '"
						+ dateFormat.format(new Date()) + "%'");
		List<SBCL_DATA> hqRList = FDbManager.getInstance(getActivity()).findAllByWhere(SBCL_DATA.class,
				"IDCARD = '" + userInfo.getIDCARD() + "' and " + "CL_TYPE='理邦心电诊断结果'" + " and " + "CL_TIME like '"
						+ dateFormat.format(new Date()) + "%'");
		if (assistChecks.size() == 0) {
			if (xtList.size() > 0) {
				etFPGL.setText(xtList.get(xtList.size() - 1).getCL_VALUE().split("mmol/L")[0]);
			}
			if (dbzList.size() > 0) {
				etNiaoChangGui1.setText(dbzList.get(dbzList.size() - 1).getCL_VALUE());
				etNiaoChangGui2.setText(pttList.get(pttList.size() - 1).getCL_VALUE());
				etNiaoChangGui3.setText(ttList.get(ttList.size() - 1).getCL_VALUE());
				etNiaoChangGui4.setText(qxList.get(qxList.size() - 1).getCL_VALUE());
			}
			if (hqRList.size() > 0) {
				String ecg_value = hqRList.get(hqRList.size() - 1).getCL_VALUE();
				// et_xindiantu.setText();
				setLiBangECG(ecg_value, hqPList.get(hqPList.size() - 1).getCL_TIME());
			}
			return;
		}
		from = 1;
		ARCHIVE_ASSISTCHECK asck = assistChecks.get(assistChecks.size() - 1);

		// etHB.setText(asck.getHB() == I_NULL ? "" : asck.getHB() + "");
		etHB.setText(asck.getHB() + "");
		etWBC.setText(asck.getWBC() + "");
		etPLT.setText(asck.getPLT() + "");
		etBLOOD_OTHER.setText(asck.getBLOOD_OTHER());
		if (!TextUtils.isEmpty(asck.getFPGL())) {
			etFPGL.setText(asck.getFPGL() + "");
		} else if (xtList.size() > 0) {
			etFPGL.setText(xtList.get(xtList.size() - 1).getCL_VALUE().split("mmol/L")[0]);
		} else {
			etFPGL.setText("");
		}

		if (!TextUtils.isEmpty(asck.getFPGDL())) {
			etFPGL2.setText(asck.getFPGDL());
		}
		if (hqRList.size() > 0) {
			String ecg_value = hqRList.get(hqRList.size() - 1).getCL_VALUE();
			// et_xindiantu.setText();
			setLiBangECG(ecg_value, hqPList.get(hqPList.size() - 1).getCL_TIME());
		}

		str_ecgId = asck.getECG();
		if (!TextUtils.isEmpty(str_ecgId)) {
			if (str_ecgId.equals("DM100-15_1")) {
				// 正常
				rd_normal.setChecked(true);
				et_xindiantu.setEnabled(false);
				et_xindiantu.setText("");
			} else if (str_ecgId.equals("DM100-15_2")) {
				// 异常
				rd_unnormal.setChecked(true);
				et_xindiantu.setEnabled(true);
				et_xindiantu.setText(asck.getECG_EX());

			}
		}
		rba_FOB.setChecked(asck.getFOB());// 大便潜血
		rba_HBSAG.setChecked(asck.getHBSAG());// 乙型肝炎表面抗原

		// 尿蛋白/ 尿糖/ 尿酮体/ 尿潜血
		if (asck.getPRO() != null && asck.getGLU() != null && asck.getKET() != null && asck.getBLD() != null) {

			etNiaoChangGui1.setText(asck.getPRO());
			etNiaoChangGui2.setText(asck.getGLU());
			etNiaoChangGui3.setText(asck.getKET());
			etNiaoChangGui4.setText(asck.getBLD());
		} else if (dbzList.size() > 0) {
			etNiaoChangGui1.setText(dbzList.get(dbzList.size() - 1).getCL_VALUE());
			etNiaoChangGui2.setText(pttList.get(pttList.size() - 1).getCL_VALUE());
			etNiaoChangGui3.setText(ttList.get(ttList.size() - 1).getCL_VALUE());
			etNiaoChangGui4.setText(qxList.get(qxList.size() - 1).getCL_VALUE());
		}
		//
		// String ncg="";
		// if(asck.getPRO()!=null){
		// }
		//
		etURINE_OTHER.setText(asck.getURINE_OTHER());
		etALBUMIN.setText(asck.getALBUMIN() + "");
		etHBALC.setText(asck.getHBALC() + "");
		// *---------------------
		// 肝功能---------------------
		etSGPT.setText(asck.getSGPT() + "");
		etGOT.setText(asck.getGOT() + "");
		etBP.setText(asck.getBP() + "");
		etCB.setText(asck.getCB() + "");
		etTBIL.setText(asck.getTBIL() + "");
		// 肾功能---------------------
		etSCR.setText(asck.getSCR() + "");
		etBUN.setText(asck.getBUN() + "");
		etPC.setText(asck.getPC() + "");
		etHYPE.setText(asck.getHYPE() + "");
		// 血脂---------------------
		etTC.setText(asck.getTC() + "");
		etLOW_CHO.setText(asck.getLOW_CHO() + "");
		etTG.setText(asck.getTG() + "");
		etHEI_CHO.setText(asck.getHEI_CHO() + "");
		// *------------------------
		// rba_XiongBuXXianPian.setChecked(asck.getCHESTX());
		if (!TextUtils.isEmpty(asck.getCHESTX())) {
			if ("DM100-15_1".equals(asck.getCHESTX())) {
				rb_xbzc.setChecked(true);
			} else if ("DM100-15_2".equals(asck.getCHESTX())) {
				rb_xbyc.setChecked(true);
			}
		}
		if (!TextUtils.isEmpty(asck.getBCHAO())) {
			if ("DM100-15_1".equals(asck.getBCHAO())) {
				rb_bczc.setChecked(true);
			} else if ("DM100-15_2".equals(asck.getBCHAO())) {
				rb_bcyc.setChecked(true);
			}
		}
		if (!TextUtils.isEmpty(asck.getCERVIX())) {
			if ("DM100-15_1".equals(asck.getCERVIX())) {
				rb_gjzc.setChecked(true);
			} else if ("DM100-15_2".equals(asck.getCERVIX())) {
				rb_gjyc.setChecked(true);
			}
		}
		if (!TextUtils.isEmpty(asck.getFbBchao())) {
			if ("DM100-15_1".equals(asck.getBCHAO())) {
				rb_bcqtzc.setChecked(true);
			} else if ("DM100-15_2".equals(asck.getBCHAO())) {
				rb_bcqtyc.setChecked(true);
			}
		}

		et_xiongbuxxianpian.setText(asck.getCHESTX_EX());

		// rba_BChao.setChecked(asck.getBCHAO());

		et_bchao.setText(asck.getBCHAO_EX());

		// rba_fbBChao.setChecked(asck.getFbBchao());
		et_fbbchao.setText(asck.getFbBchao_EX());

		// rba_GongJingTuPian.setChecked(asck.getCERVIX());
		et_gongjingtupian.setText(asck.getCERVIX_EX());
		ed_qt_last.setText(asck.getOTHER());
	}

	/**
	 * 保存数据
	 * 
	 * @author 鹿云龙
	 * @date 2015年12月21日 上午10:49:31
	 */
	private void saveData() {
		assistCheck = new ARCHIVE_ASSISTCHECK();
		assistCheck.setIDCARD(userInfo.getIDCARD());// 身份证号
		// 1
		String hb = etHB.getText().toString();
		assistCheck.setHB(hb);// 血红蛋白
		String wbc = etWBC.getText().toString();
		assistCheck.setWBC(wbc);// 白细胞
		String plt = etPLT.getText().toString();
		assistCheck.setPLT(plt);// 血小板
		assistCheck.setBLOOD_OTHER(etBLOOD_OTHER.getText().toString());// 血常规其他
		// DecimalFormat df = new DecimalFormat(".00");//.00就表示保留后两位数

		// String fpgdl = df.format(etFPGDL.getText().toString());
		// assistCheck.setFPGDL(fpgdl.isEmpty() ? I_NULL :
		// Integer.parseInt(fpgdl));// 空腹血糖
		if (!TextUtils.isEmpty(etFPGL.getText())) {
			assistCheck.setFPGL(etFPGL.getText().toString());// 空腹血糖
		}

		if (!TextUtils.isEmpty(etFPGL2.getText())) {
			assistCheck.setFPGDL(etFPGL2.getText().toString());// 空腹血糖dl
		}
		// etFPGL2/18
		String ncg="";
		if(!TextUtils.isEmpty(etNiaoChangGui1.getText())&&!TextUtils.isEmpty(etNiaoChangGui2.getText())&&!TextUtils.isEmpty(etNiaoChangGui3.getText())&&!TextUtils.isEmpty(etNiaoChangGui4.getText())){
			ncg = etNiaoChangGui1.getText().toString()+"/"+etNiaoChangGui2.getText().toString()+"/"+etNiaoChangGui3.getText().toString()+"/"+etNiaoChangGui4.getText().toString();
		}

		if (!ncg.isEmpty()) {
			if (ncg.contains("/")) {
				String[] ncgs = ncg.split("/");
				if (ncgs.length == 4) {
					assistCheck.setPRO(ncgs[0]);// 尿蛋白
					assistCheck.setGLU(ncgs[1]);// 尿糖
					assistCheck.setKET(ncgs[2]);// 尿酮体
					assistCheck.setBLD(ncgs[3]);// 尿潜血
				}else {
					((TiJianActivity) getActivity()).showTextDialog("填写的尿常规数据不符合规范");
					return;
				}
			}else{
				((TiJianActivity) getActivity()).showTextDialog("填写的尿常规数据不符合规范");
				return;
			}
		}
		assistCheck.setURINE_OTHER(etURINE_OTHER.getText().toString());// 尿常规其他
		String albumin = etALBUMIN.getText().toString();
		assistCheck.setALBUMIN(albumin);// 尿微量蛋白
		String hbalc = etHBALC.getText().toString();
		assistCheck.setHBALC(hbalc);// 糖化血红蛋白

		if (rd_normal.isChecked()) {
			str_ecgId = "DM100-15_1";
		} else if (rd_unnormal.isChecked()) {
			str_ecgId = "DM100-15_2";
		} else {
			str_ecgId = "";
		}

		assistCheck.setECG(str_ecgId);
		assistCheck.setFOB(rba_FOB.getCheckedBD().getID());
		assistCheck.setHBSAG(rba_HBSAG.getCheckedBD().getID());
		assistCheck.setECG_EX(et_xindiantu.getText().toString());

		// 肝功能
		String sgpt = etSGPT.getText().toString();
		assistCheck.setSGPT(sgpt);// 血清谷丙转氨酶
		String got = etGOT.getText().toString();
		assistCheck.setGOT(got);// 血清谷草转氨酶
		String bp = etBP.getText().toString();
		assistCheck.setBP(bp);// 白蛋白
		String cb = etCB.getText().toString();
		assistCheck.setCB(cb);// 结合胆红素
		String tbil = etTBIL.getText().toString();
		assistCheck.setTBIL(tbil);// 总胆红素

		// 肾功能
		String scr = etSCR.getText().toString();
		assistCheck.setSCR(scr);// 血清肌酐
		String bun = etBUN.getText().toString();
		assistCheck.setBUN(bun);// 血尿素氮
		String pc = etPC.getText().toString();
		assistCheck.setPC(pc);// 血钾浓度
		String hype = etHYPE.getText().toString();
		assistCheck.setHYPE(hype);// 血钠浓度

		// 血脂
		String tc = etTC.getText().toString();
		assistCheck.setTC(tc);// 总胆固醇
		String lowCho = etLOW_CHO.getText().toString();
		assistCheck.setLOW_CHO(lowCho);// 血清低密度脂蛋白胆固醇
		String tg = etTG.getText().toString();
		assistCheck.setTG(tg);// 甘油三酯
		String heiCho = etHEI_CHO.getText().toString();
		assistCheck.setHEI_CHO(heiCho);// 血清高密度脂蛋白胆固醇

		// *DM100-15_1
		// 胸部
		if (rb_xbzc.isChecked()) {
			assistCheck.setCHESTX("DM100-15_1");
		} else if (rb_xbyc.isChecked()) {
			assistCheck.setCHESTX("DM100-15_2");
		}
		// 宫颈
		if (rb_gjzc.isChecked()) {
			assistCheck.setCERVIX("DM100-15_1");
		} else if (rb_gjyc.isChecked()) {
			assistCheck.setCERVIX("DM100-15_2");
		}
		// B超
		if (rb_bczc.isChecked()) {
			assistCheck.setBCHAO("DM100-15_1");
		} else if (rb_bcyc.isChecked()) {
			assistCheck.setBCHAO("DM100-15_2");
		}
		// B超其他
		if (rb_bcqtzc.isChecked()) {
			assistCheck.setFbBchao("DM100-15_1");
		} else if (rb_bcqtyc.isChecked()) {
			assistCheck.setFbBchao("DM100-15_2");
		}

		// assistCheck.setCHESTX(rba_XiongBuXXianPian.getChecked());
		// assistCheck.setBCHAO(rba_BChao.getChecked());
		// assistCheck.setFbBchao(rba_fbBChao.getChecked());
		// assistCheck.setCERVIX(rba_GongJingTuPian.getChecked());

		assistCheck.setCHESTX_EX(et_xiongbuxxianpian.getText().toString());
		assistCheck.setBCHAO_EX(et_bchao.getText().toString());
		assistCheck.setFbBchao_EX(et_fbbchao.getText().toString());
		assistCheck.setCERVIX_EX(et_gongjingtupian.getText().toString());
		assistCheck.setOTHER(ed_qt_last.getText().toString());
		if (from == 0) {
			FDbManager.getInstance(getActivity()).save(assistCheck);
			from = 1;
		} else {
			FDbManager.getInstance(getActivity()).updata(assistCheck, "IDCARD='" + userInfo.getIDCARD() + "'");
		}
		((TiJianActivity) getActivity()).showTextDialog("保存成功");
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(getActivity(), ExamHistoryActivity.class);
		int id = v.getId();
		if (id == R.id.niaochanggui_btn) {
			intent.putExtra("flag", BluetoothConstant.NIAOCHANGGUI_TYPENAME);
			getActivity().startActivityForResult(intent, GlobalConts.RequestNiaoChangGui);
		} else if (id == R.id.xuetang_btn) {
			intent.putExtra("flag", BluetoothConstant.XUETANG_TYPENAME);
			getActivity().startActivityForResult(intent, GlobalConts.RequestXuetang);
		} else if (id == R.id.xhdb_btn) {
			intent.putExtra("flag", BluetoothConstant.HB_TYPENAME);
			getActivity().startActivityForResult(intent, GlobalConts.RequestXueHongDanbai);
		} else if (id == R.id.ecg_btn) {
			// 虽然请求是 理邦心电诊断结果，但是实际原因是 华清心电的采集结果存的就是 理邦心电（对于设备本身来讲都是心电）
			// intent.putExtra("flag", "华清心电诊断结果");
			intent.putExtra("flag", BluetoothConstant.ECG_TYPENAME);
			getActivity().startActivityForResult(intent, GlobalConts.RequestECG);
		}  else if (id == R.id.dgc_btn) {
			//胆固醇
			intent.putExtra("flag", BluetoothConstant.DGC_TYPENAME);
			getActivity().startActivityForResult(intent, GlobalConts.RequestDGC);
		} else {


		}

	}

	@Override
	public void setXuetang(String value) {
		etFPGL.setText(value);
	}

	@Override
	public void setXueHongDanBai(String xhdb) {
		if (!TextUtils.isEmpty(xhdb)) {
			// g/dL *10 =  g/L
			etHB.setText(String.valueOf((int) (Double.parseDouble(xhdb.replace("g/dL","")) * 10)));
		}
	}

	@Override
	public void setNiaoYeFenxi(String niaoDanYuan, String qianXue, String DanHongSu, String tongti, String puTaoTang,
			String danBaiZhi, String PH, String yaXiaoSuan, String baiXiBao, String biZhong, String weiShengSu) {
		etNiaoChangGui1.setText(danBaiZhi);
		etNiaoChangGui2.setText(puTaoTang);
		etNiaoChangGui3.setText(tongti);
		etNiaoChangGui4.setText(qianXue);
	}

	@Override
	public void setLiBangECG(String libangEcgResult, String time) {

		et_xindiantu.setText(libangEcgResult);
		if (libangEcgResult.contains("正常")||libangEcgResult.contains("波形未见异常")) {
			rd_normal.setChecked(true);
		} else {
			rd_unnormal.setChecked(true);
		}

		final String jpg_url = ExamHistoryActivity.getXinDianPicture(time);
		if (!TextUtils.isEmpty(jpg_url)) {
			File file_img = new File(jpg_url);
			if (file_img.exists()) {
				tv_ecg.setTextColor(Color.BLUE);
				tv_ecg.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
				// 心电图点击事件
				tv_ecg.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(getActivity(), ShowPicActivity.class);
						intent.putExtra("JPG_URL", jpg_url);
						startActivity(intent);
					}
				});
			}

		} else {
			tv_ecg.setTextColor(Color.GRAY);
			tv_ecg.setPaintFlags(0);
			// 心电图点击事件
			tv_ecg.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					((TiJianActivity) getActivity()).showTextDialog("没有相关的心电图");
				}
			});
		}

	}

	// 显示血红蛋白
	public static void showXHDB(String value) {
		if (etHB != null && !TextUtils.isEmpty(value)) {
			etHB.setText(value);
		}
	}

	// // 显示血糖
	// public static void showXuetangol(String value) {
	// if (etFPGL != null && !TextUtils.isEmpty(value)) {
	// etFPGL.setText(value);
	// }
	//
	// }

	// 显示尿液分析
//	public static void showNiaoYeFenxi(String danBaiZhi, String puTaoTang, String tongti, String qianxue) {
//		if (etNiaoChangGui != null && !TextUtils.isEmpty(danBaiZhi)) {
//			etNiaoChangGui.setText(danBaiZhi + "/" + puTaoTang + "/" + tongti + "/" + qianxue);
//		}
//	}

    @Override
    public void setCHOL(String dgc) {
        if (!TextUtils.isEmpty(dgc)) {
            etTC.setText(dgc);
        }
    }

    @Override
	public void onDestroy() {
		super.onDestroy();
		saveData();
	}

}